/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.TX;

import entity.GroupBuyingItem;
import entity.PurchasedItem_GroupOn;
import entity.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Lu Xiaodi
 */
@Stateless
public class GroupBuyCustomerSB implements GroupBuyCustomerSBRemote {

    @PersistenceContext
    private EntityManager em;

    public List<GroupBuyingItem> getAvailableGBItem() {
        Date date = new Date();
        Query query = em.createQuery("SELECT g FROM GroupBuyingItem g WHERE g.effectiveEnd > :today"
                + " AND g.effectiveStart< :today AND g.isApproved= :approved");
        query.setParameter("today", date);
        query.setParameter("approved", true);
        return query.getResultList();
    }

    public Long getCustId(String customerEmail) {
        Query query = em.createQuery("SELECT u from Users u WHERE u.email = :value");
        query.setParameter("value", customerEmail);
        List<Users> list = query.getResultList();
        if (list.size() > 1) {
            // throw exception
        }
        return list.get(0).getId();
    }

//    public void buyItem(String customerEmail, GroupBuyingItem selectedItem, Integer numItems){
//        Long custId = this.getCustId(customerEmail);
//        for (int i=0; i<numItems; i++){
//            selectedItem.addBuyer(custId); 
//        }
//    }
    public List<Integer> getQuotaSeq(GroupBuyingItem selectedItem, Long customerId) {
        // get personal quota
        Integer personalQuota = selectedItem.getPersonalQuota();

        // set personal quota to the smaller one in [quota left, personal quota]
        Query queryQty = em.createQuery("SELECT p FROM PurchasedItem_GroupOn p WHERE "
                + "p.item_id = :itemId");
        queryQty.setParameter("itemId", selectedItem.getId());
        List<PurchasedItem_GroupOn> purchasedItems = queryQty.getResultList();
        Integer quotaOccupied = 0;
        for (PurchasedItem_GroupOn p : purchasedItems) {
            quotaOccupied += p.getQty();
        }
        personalQuota = ((selectedItem.getMaxQuota() - quotaOccupied) >= personalQuota
                ? personalQuota : selectedItem.getMaxQuota() - quotaOccupied);

        // get numItems the person has purchased
        Integer usedQuota = 0;
        Query query = em.createQuery("SELECT p From PurchasedItem_GroupOn p WHERE "
                + "p.customerId = :custId");
        query.setParameter("custId", customerId);
        List<PurchasedItem_GroupOn> pastPurchase = query.getResultList();
        if (pastPurchase != null) {
            for (PurchasedItem_GroupOn p : pastPurchase) {
                usedQuota += p.getQty();
            }
        }

        // set personal quota to personal quota = used quota
        personalQuota -= usedQuota;

        // get thie number sequence available for purchase
        List<Integer> list = new ArrayList<Integer>();
        if (personalQuota <= 0) {
            list.add(0);
        } else {
            for (int i = 1; i <= personalQuota; i++) {
                list.add(i);
            }
        } 
        return list;
    }
}
